﻿namespace LF.Common
{
    public struct MonthOnly
	{
		public int Year { get; set; }
		public int Month { get; set; }
        public override string ToString()
        {
            return $"{Year.ToString().PadLeft(4, '0')}-{Month.ToString().PadLeft(2, '0')}";
        }
        public static MonthOnly Parse(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return new MonthOnly { Year = 1949, Month = 1 };
            }
            var date = DateOnly.Parse(str);
            return new MonthOnly { Year = date.Year, Month = date.Month };
        }
        public static MonthOnly Now
        {
            get
            {
                return new MonthOnly { Year = DateTime.Now.Year, Month = DateTime.Now.Month };
            }
        }

        public static bool operator == (MonthOnly a ,MonthOnly b)
        {
            return a.Year == b.Year && a.Month == b.Month;
        }
        public static bool operator !=(MonthOnly a, MonthOnly b)
        {
            return a.Year != b.Year || a.Month != b.Month;
        }

        public override bool Equals(object? obj)
        {
            var b = Parse(obj?.ToString() ?? "");
            return this == b;
        }

        public override int GetHashCode()
        {
            return new DateOnly(Year, Month, 1).GetHashCode();
        }
    }
}

